package com.apache.bid.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.bid.entity.BidPrintParam;
import com.apache.bid.manager.BidPrintParamManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;

public class BidPrintParamManagerImpl implements BidPrintParamManager {

	protected IDao bidPrintParamDao;
	protected final String entityName = "com.apache.bid.entity.BidPrintParam";

	public void setBidPrintParamDao(IDao bidPrintParamDao) {
		this.bidPrintParamDao = bidPrintParamDao;
	}

	public String saveInfo(ParamsVo<BidPrintParam> vo) throws BusinessException {
		BidPrintParam bidPrintParam = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="bidPrintParam_"+infoId;
		bidPrintParam.setPrintId(infoId);
		MethodParam param = new MethodParam("BidPrintParam", cacheKey, "", entityName);
		param.setVaule(bidPrintParam);
		if (bidPrintParamDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<BidPrintParam> vo) throws BusinessException {
		BidPrintParam bidPrintParam = vo.getObj();
		if (Validator.isNotNull(bidPrintParam.getPrintId())) {
			String cacheKey = "";
			//String cacheKey="bidPrintParam_"+bidPrintParam.getPrintId();
			MethodParam param = new MethodParam("BidPrintParam", cacheKey, "", entityName);
			param.setVaule(bidPrintParam);
			return bidPrintParamDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<BidPrintParam> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="bidPrintParam_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		BidPrintParam info = (BidPrintParam) bidPrintParamDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("BidPrintParam");
			return bidPrintParamDao.edit(param);
		} else {
			param.setParams("printId", infoId);
			param.setDelete(true);
			return bidPrintParamDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<BidPrintParam> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidPrintParam_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return bidPrintParamDao.selectById(param);
	}

	public Object execute(ParamsVo<BidPrintParam> vo) {
		String key = vo.getMethodKey();
		if ("singleParaByAttr".equalsIgnoreCase(key)) {
			return this.getSingleParaByAttr(vo.getObj());
		}
		return null;
	}

	private BidPrintParam getSingleParaByAttr(BidPrintParam bidPrintPara) {
		MethodParam param = new MethodParam("ByProperty", "", "", entityName);
		StringBuffer sb = new StringBuffer();
		if (Validator.isNotNull(bidPrintPara.getSessionId()) && !"0".equals(bidPrintPara.getSessionId())) {
			sb.append(" and sessionId =:sessionId");
			param.setParams("sessionId", bidPrintPara.getSessionId());
		}
		if (Validator.isNotNull(bidPrintPara.getItemId()) && !"0".equals(bidPrintPara.getItemId())) {
			sb.append(" and itemId =:itemId");
			param.setParams("itemId", bidPrintPara.getItemId());
		}
		if (Validator.isNotNull(bidPrintPara.getPrintId())) {
			sb.append(" and printId =:printId");
			param.setParams("printId", bidPrintPara.getPrintId());
		}
		if (!Validator.isNull(sb.toString())) {
			param.setSqlStr(bidPrintParamDao.getSql(2) + sb.toString());
			BidPrintParam bidPara = (BidPrintParam) bidPrintParamDao.selectSingle(param);
			return bidPara;
		} else {
			return null;
		}
	}

	public Page getPageInfo(ParamsVo<BidPrintParam> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by create_time desc ");
		param.setParams("orderBy", orderBy);
		Page page = bidPrintParamDao.pageSelect(param);
		return page;
	}

	public List<BidPrintParam> getList(ParamsVo<BidPrintParam> vo) {
		MethodParam param = setMethodParams(vo, 2);
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by create_time desc ");
		param.setParams("orderBy", orderBy);
		return bidPrintParamDao.select(param);
	}

	public long countInfo(ParamsVo<BidPrintParam> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return bidPrintParamDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<BidPrintParam> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(bidPrintParamDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		BidPrintParam bidPrintParam = vo.getObj();
		param.setResultCache(false);
		if (Validator.isNotNull(bidPrintParam.getItemPayDay())) {
			sb.append(" and itemPayDay = :itemPayDay");
			param.setParams("itemPayDay", bidPrintParam.getItemPayDay());
		}
		if (Validator.isNotNull(bidPrintParam.getItemId())) {
			sb.append(" and itemId = :itemId");
			param.setParams("itemId", bidPrintParam.getItemId());
		}
		if (Validator.isNotNull(bidPrintParam.getItemName())) {
			sb.append(" and itemName = :itemName");
			param.setParams("itemName", bidPrintParam.getItemName());
		}
		if (Validator.isNotNull(bidPrintParam.getSessionId())) {
			sb.append(" and sessionId = :sessionId");
			param.setParams("sessionId", bidPrintParam.getSessionId());
		}
		if (Validator.isNotNull(bidPrintParam.getSessionName())) {
			sb.append(" and sessionName = :sessionName");
			param.setParams("sessionName", bidPrintParam.getSessionName());
		}
		if (Validator.isNotNull(bidPrintParam.getPayBail())) {
			sb.append(" and payBail = :payBail");
			param.setParams("payBail", bidPrintParam.getPayBail());
		}
		if (Validator.isNotNull(bidPrintParam.getPayAddress())) {
			sb.append(" and payAddress = :payAddress");
			param.setParams("payAddress", bidPrintParam.getPayAddress());
		}
		if (Validator.isNotNull(bidPrintParam.getPayNotice())) {
			sb.append(" and payNotice = :payNotice");
			param.setParams("payNotice", bidPrintParam.getPayNotice());
		}
		if (Validator.isNotNull(bidPrintParam.getTemplateId())) {
			sb.append(" and templateId = :templateId");
			param.setParams("templateId", bidPrintParam.getTemplateId());
		}
		if (Validator.isNotNull(bidPrintParam.getCreateTime())) {
			sb.append(" and createTime = :createTime");
			param.setParams("createTime", bidPrintParam.getCreateTime());
		}
		if (Validator.isNotNull(bidPrintParam.getModifyTime())) {
			sb.append(" and modifyTime = :modifyTime");
			param.setParams("modifyTime", bidPrintParam.getModifyTime());
		}
		if (Validator.isNotNull(bidPrintParam.getSpare1())) {
			sb.append(" and spare1 = :spare1");
			param.setParams("spare1", bidPrintParam.getSpare1());
		}
		if (Validator.isNotNull(bidPrintParam.getSpare2())) {
			sb.append(" and spare2 = :spare2");
			param.setParams("spare2", bidPrintParam.getSpare2());
		}
		if (Validator.isNotNull(bidPrintParam.getSpare3())) {
			sb.append(" and spare3 = :spare3");
			param.setParams("spare3", bidPrintParam.getSpare3());
		}
		if (Validator.isNotNull(bidPrintParam.getSpare4())) {
			sb.append(" and spare4 = :spare4");
			param.setParams("spare4", bidPrintParam.getSpare4());
		}
		if (Validator.isNotNull(bidPrintParam.getSpare5())) {
			sb.append(" and spare5 = :spare5");
			param.setParams("spare5", bidPrintParam.getSpare5());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
